package com.shxc.smarttrade.community.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.community.model.CommunityNewsModel;
import com.shxc.smarttrade.community.model.CommunityNewsModel.DataBean;
import com.shxc.smarttrade.utils.AppUtils;
import com.shxc.smarttrade.utils.GlideUtils;
import com.shxc.smarttrade.utils.Html2Text;
import com.shxc.smarttrade.utils.Loger;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 社区 - 新闻
 */

public class AndroidCommunityNewsAdapter extends RecyclerView.Adapter<AndroidCommunityNewsAdapter.ViewHolder> {

    private Context mContext;
    private List<CommunityNewsModel.DataBean> mList;
    private Handler mHandler;
    private GlideUtils glideUtils;

    public AndroidCommunityNewsAdapter(Context c, List<CommunityNewsModel.DataBean> list, Handler handler) {
        mContext = c;
        mList = list;
        mHandler = handler;
        glideUtils = new GlideUtils(c);
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_community_news, parent, false);
        ViewHolder holder = new ViewHolder(inflate);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        final CommunityNewsModel.DataBean model = mList.get(position);

        Log.e("title",model.getTitle());
        Log.e("Introduce",model.getIntroduce());

        holder.title_textview.setText(model.getTitle());
        holder.content_textview.setText(model.getIntroduce());


        if (AppUtils.isEmpty(model.getImgURL())) {
            holder.imageview.setVisibility(View.GONE);
//            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)holder.content_textview.getLayoutParams();
//            layoutParams.setMargins(100,20,10,5);//4个参数按顺序分别是左上右下
        }else{
            Loger.e("url",model.getImgURL());
            holder.imageview.setVisibility(View.VISIBLE);
            glideUtils.showFillet(model.getImgURL(),holder.imageview,5);
        }
        holder.sourse_textview.setText("来源：SmartTrade");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        holder.time_textview.setText(sdf.format(new Date(model.getTime())));

        //跳转页面
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message msg = new Message();
                msg.what = 1;
                msg.obj = model;
                mHandler.sendMessage(msg);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public void upDate(List<DataBean> list) {
        mList = list;
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        protected TextView title_textview;
        protected TextView content_textview;
        protected ImageView imageview;
        protected TextView sourse_textview;
        protected TextView time_textview;

        public ViewHolder(View itemView) {
            super(itemView);
            title_textview = itemView.findViewById(R.id.title_textview);
            content_textview = itemView.findViewById(R.id.content_textview);
            imageview = itemView.findViewById(R.id.imageview);
            sourse_textview = itemView.findViewById(R.id.sourse_textview);
            time_textview = itemView.findViewById(R.id.time_textview);
        }
    }

}
